<%@ include file="/jsp/common.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<base href="${path}">
    <title>添加新商品</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript" src="./js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="./js/common.js"></script>
<style type="text/css">
input {
text-align: left;

}


</style>

<script type="text/javascript">
	function addRow(){	
		$("#tableGoods").append(goodsHtml);	
		
		}
	function deleteRow(){
		var che=$("#tableGoods input:checked");
		for ( var i = 0; i < che.length; i++) {
			var node=che[i].parentNode.parentNode;
			$(node).remove();
		}
		
		}
	function addGoods(){
		var data=getData();		
		$.ajax({
			   type: "POST",
			   url: getPath()+"goodsJson/addGoods.do", 
			   data:{"data":data},
			   dataType:"json",
			   success: function(data, textStatus){
				   var content=eval('('+data+')');
				   alert(data);
			   },
			   error:function(XMLHttpRequest, textStatus, errorThrown){
				   alert(errorThrown);
				 }
			});	
		}
	function getData(){
		var tr=$("#tableGoods tr");
		var json="[";
		for ( var i = 1; i < tr.length; i++) {
			var input=$("#tableGoods tr:eq("+i+") input,select");
			for ( var j = 0; j < input.length; j++) {
				var goodsName;
				var goodsUrl;
				var goodsImgUrl;
				var spreadTypeId;
				var goodsTypeId;
				var userId;
				var other;
				
				if($(input[j]).attr("name")=="goodsName"){
				goodsName=$(input[j]).val();
				}
				
				if($(input[j]).attr("name")=="goodsUrl"){
					goodsUrl=$(input[j]).val();
				}
				
				if($(input[j]).attr("name")=="goodsImgUrl"){
					goodsImgUrl=$(input[j]).val();
				}
				
				if($(input[j]).attr("name")=="spreadTypeId"){
					spreadTypeId=$(input[j]).val();
				}
				
				if($(input[j]).attr("name")=="goodsTypeId"){
					goodsTypeId=$(input[j]).val();
				}
				
				if($(input[j]).attr("name")=="userId"){
					userId=$(input[j]).val();
				}
				if($(input[j]).attr("name")=="other"){
					other=$(input[j]).val();
				}
			}
			var goods=new Goods(goodsName,goodsUrl,goodsImgUrl,spreadTypeId,goodsTypeId,userId,other);
			if(i!=tr.length-1){				
			json+=goods.toString+",";

				}else{
					json+=goods.toString;
					}
		}
		json+="]";  
		return json;
		}
	function Goods(goodsName,goodsUrl,goodsImgUrl,spreadTypeId,goodsTypeId,userId,other){
		this.toJson={
			"goodsName":goodsName,
			"goodsUrl":goodsUrl,
			"goodsImgUrl":goodsImgUrl,
			"spreadTypeId":spreadTypeId,
			"goodsTypeId":goodsTypeId,
			"userId":userId,
			"other":other
			};
		this.toString= "{\"goodsName\":"+goodsName+",\"goodsUrl\":"+goodsUrl+",\"goodsImgUrl\":"+goodsImgUrl+",\"spreadTypeId\":"+spreadTypeId+",\"goodsTypeId\":"+goodsTypeId+",\"userId\":"+userId+",\"other\":"+other+"}";
		}
	function checkData(goods){
		var goodsUrl=false;
		var key=goods.spreadTypeId;
		switch(key){
		case 1:
			if(goods.goodsUrl.indexOf("http://www.taobao.com")!=0){
				goodsUrl=true;
				}
			break;
		case 2:
			if(goods.goodsUrl.indexOf("http://s.click.taobao.com")!=0){
				goodsUrl=true;
				}
			break;
		case 3:
			if(goods.goodsUrl.indexOf("http://redirect.simba.taobao.com")!=0){
				goodsUrl=true;
				}
			break;
		}
		if(!goodsUrl){
		alert("商品地址不合法！");
			}
	}

	
	var goodsHtml;
	$(document).ready(function(){	
		$('#tableGoods tr').hover(function(){
		    $(this).children('td').addClass('hover')
		}, function(){
		    $(this).children('td').removeClass('hover');
		});
		goodsHtml=$("#tableGoods tr:last")[0].outerHTML; 
	});
</script>
  </head>
  
  <body>
    <center>
    	<h1>添加新商品</h1>
    	<button onclick="addRow()">增加行</button>
    	<button onclick="deleteRow()">删除行</button>
    	<button onclick="addGoods()">保存</button>
    	<table id="tableGoods"  width="1200px" border="1">
    	<tr>
    	<td  style="width:10px;"></td> 
    	<td style="width:200px;">商品名称</td>
    	<td style="width:200px;">商品连接</td>
    	<td style="width:200px;">商品图片连接</td>
    	<td style="width:200px;">商品类型</td>
    	<td style="width:50px;">用户ID</td> 
    	<td style="width:50px;">店铺ID</td>
    	<td style="width:200px;">备注</td>
    	</tr>
    	<tr>
    	<td><input type="checkbox"/></td>
    	<td><input type="text" name="goodsName" value=""/></td>
    	<td><input type="text" name="goodsUrl"  value=""/></td>
    	<td><input type="text" name="goodsImgUrl"  value=""/></td>
    	
    	<td>
    		<select name="spreadTypeId">
    		<option value="1">活动</option>
    		<option value="2">店铺</option>
    		<option value="3">衣服</option>   		
    	</select>
    	</td>
    	
    	<td>
    	<select name="goodsTypeId">
    		<option value="3">衣服</option>
    		<option value="4">数码</option>
    		<option value="5">医药</option>
    		<option value="6">图书</option>
    		<option value="7">百货</option>
    		<option value="8">其他</option>
    	</select>
    	</td>
    	<td><input type="text" name="userId"  value=""/></td>   	
    	<td><input type="text" name="other"  value=""/></td>
    	</tr>	    		
    	</table>
    
    
    </center>
  </body>
</html>
